namespace("jclass.core")
({
    Singleton : jClass({
        constructor : function() {
            if (this._prototype.extending.length !== 0) {
                var classInstance = getClassInstance(arguments);
                if (classInstance === undefined || this._prototype.extending.indexOf(classInstance) == -1) {
                    throw("Cannot instantiate a singleton object. Please use `getInstance()` instead");
                }
            } 
        },
        static : {
            getInstance : function() {
               if (_.inst === undefined) _.inst = {};
               if (_.inst[this._typeId] === undefined) {
                   var cls = new this(this._typeId)
                   _.inst[this._typeId] = cls;
               }
               return _.inst[this._typeId];
            }
        },
        private : {
            getClassInstance : function(obj, depth) {
                if (depth === undefined) depth = 0;
                if (typeof obj.callee == 'function') {
                    var object = obj;
                    for (var i = 0; i < depth; i++) {
                        if (object[0] === undefined) {
                            return undefined
                        }
                        object = object[0];
                    }
                    if (typeof object[0] !== 'number') {
                        return getClassInstance(object, ++depth)
                    } else {
                        return object[0]
                    }
                }
            }
        }
    })

});